(function($) {
	var $this = null;

	var pre = "serviceListPanel";
	var url = "../service/selecter";
	var heads = ["serviceId", "serviceName", "serviceCode"];

	$(function() {
		$("#" + pre + "_close").bind("click", closePanel);
		$("#" + pre + "_search").bind("click", search);
	});

	function openPanel(self) {
		$this = $(self);
		$this.blur();
		$("#" + pre + "_searchName").val('');
		search();
	}

	/**
	 * 加载数据
	 */
	function search() {
		var content = $("#" + pre + "_searchName").val();
		new Pagination({
			method : 'get',
			type : 'json',
			url : url,
			data : {
				content : content
			},
			pageSize : 3,
			startIndex : 0,
			success : printDataToPage,
			pagerHandler : pre + "_Pager",
			last : last
		});
	}

	function last(message) {
		$("#" + pre + ",#" + pre + "_yinying").show();
		addMargin($("#" + pre));
	}

	function printDataToPage(message) {
		var $tbody = $("#" + pre + "_data");
		$tbody.empty();
		var len = message.result.data.length;
		for (var i = 0; i < len; i++) {
			var heads0 = getHtmlString(getHeadIndexValue(i, message, heads[0]));
			var heads1 = getHtmlString(getHeadIndexValue(i, message, heads[1]));
			var heads2 = getHtmlString(getHeadIndexValue(i, message, heads[2]));

			var select = "<input class=\"common_btn\" type=\"button\" onclick=\"" + pre + "_Selected('" + heads0 + "','" + heads1 + "')\" value=\"选择\"></input>";
			var tr = "<tr>" + "<td>" + heads0 + "</td>" + "<td>" + heads1 + "</td>" + "<td>" + heads2 + "</td><td>" + select + "</td></tr>";
			$tbody.append($(tr));
		}
	}

	/**
	 * 关闭
	 */
	function closePanel() {
		$("#" + pre + ",#" + pre + "_yinying").hide();
	}

	/**
	 * 选择并关闭
	 */
	function selected(heads0, heads1) {
		$this.empty();
		$this.append("<option value='" + heads0 + "' selected=\"selected\" >" + heads1 + "</option>");
		$("#" + pre + ",#" + pre + "_yinying").hide();

		if (window.serviceSelected) {
			window.serviceSelected(heads1, heads2);
		}
	}

	window[pre + "_Selected"] = selected;
	window.openServiceSelecter = openPanel;
})(jQuery);
